#include <QPainter>
#include <QtDebug>

#include "tiplabel.h"

#define MENU_ROUND      10
#define ARROW_HEIGHT    18
#define TEXT_HPADDING   26
#define TEXT_VPADDING   18

TipLabel::TipLabel(QWidget *parent)
    : QWidget(parent), label("")
{
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    setAttribute(Qt::WA_TranslucentBackground);
}

TipLabel::TipLabel(const QString &text, QWidget *parent)
    : QWidget(parent), label(text)
{
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

TipLabel::~TipLabel()
{
}


QSize TipLabel::sizeHint() const
{
    int height = fontMetrics().xHeight() + TEXT_VPADDING;
    int width = fontMetrics().width(label) + TEXT_HPADDING*2;
    qDebug() << label << width << height;
    return QSize(width, height + ARROW_HEIGHT);
}

void TipLabel::paintEvent(QPaintEvent *e)
{
    qDebug() << "painting";

    int height = fontMetrics().xHeight() + TEXT_VPADDING;
    int width = fontMetrics().width(label) + TEXT_HPADDING*2;
    int midWidth = width / 2 - 5;

    QPainterPath path;
    path.addRoundedRect(0, 0, width, height, MENU_ROUND, MENU_ROUND);
    path.moveTo(midWidth, height);
    path.lineTo(midWidth + (ARROW_HEIGHT / 2), height + (ARROW_HEIGHT / 2));
    path.lineTo(midWidth + ARROW_HEIGHT, height);

    QLinearGradient brush(0, 0, 0, height);
    brush.setColorAt(0.5f, QColor(0x50, 0x51, 0x55));
    brush.setColorAt(0.0f, QColor(0x7d, 0x7e, 0x73));
    brush.setColorAt(1.0f, QColor(0x00, 0x00, 0x00));

    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    // Draw Path
    p.setPen(QPen(Qt::transparent, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    p.setBrush(brush);
    p.drawPath(path);

    p.end();
}
